import axios from "axios";
import * as vscode from 'vscode';

const request = axios.create({
  baseURL: '',
});

// 添加请求拦截器
request.interceptors.request.use(config => {
  // 动态设置 Cookie
  config.headers.Cookie = vscode.workspace.getConfiguration('zd-vscode-tools')?.yapiCookie;
  return config;
}, error => {
  return Promise.reject(error);
});

request.interceptors.response.use(
  // 请求成功
  response => {
    let data = response.data;

    if (!(data instanceof Object)) {
      try {
        data = JSON.parse(data);
      } catch (error) {
        return Promise.reject(error);
      }
    }
    return data.data;
  },
  // 请求失败
  error => {
    return Promise.reject(error.response);
  },
);


export async function getYapiInterface(params:{id: number}) {
  return request.get<any>(`https://yapi.zsdx.cn/api/interface/get?id=${params.id}`);
}